package com.xltt.vehicleControl.presenter;


import android.util.Log;

import com.xltt.vehicleControl.base.BasePresenter;
import com.xltt.vehicleControl.bean.BaseData;
import com.xltt.vehicleControl.bean.BaseResult;
import com.xltt.vehicleControl.bean.UserResponse;
import com.xltt.vehicleControl.contract.IUserContract;
import com.xltt.vehicleControl.model.UserModel;
import com.xltt.vehicleControl.util.RxJavaUtil;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class UserPresenter extends BasePresenter<IUserContract.IUserView> implements IUserContract.IUserPresenter {

    private static final String TAG = UserPresenter.class.getName();

    private BaseResult<UserResponse> mBaseResult;
    private BaseResult<BaseData> mBaseData;
    private UserModel mUserModel;

    private UserPresenter() {
        mUserModel = UserModel.getInstance();
    }

    public static UserPresenter getInstance() {
        return UserPresenter.Inner.instance;
    }

    private static class Inner {
        private static final UserPresenter instance = new UserPresenter();
    }

    @Override
    public void loginByUserAndPassWord(Map<String, String> body) {
        Observable observable = mUserModel.loginByUserAndPassWord(body).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<UserResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<UserResponse> poetryEntity) {
                mBaseResult = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mBaseResult != null) {
                    getMvpView().loginResult(mBaseResult);
                }
            }
        });
    }


    @Override
    public void logout() {
        Observable observable = mUserModel.logout().doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                addDisposable(disposable);
            }
        });
        observable = RxJavaUtil.toSubscribe(observable);
        observable.subscribe(new Observer<BaseResult<BaseData>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseResult<BaseData> poetryEntity) {
                mBaseData = poetryEntity;
            }

            @Override
            public void onError(Throwable e) {
                getMvpView().onError(e.getMessage());
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {
                if (mBaseData != null) {
                    getMvpView().logoutResult(mBaseData);
                }
            }
        });
    }



}

